<?php

/**
 * @Author:      lyt
 * @DateTime:    2016-06-27 16:41:54
 * @Description:  用于 库存的预警 
 */
class ScsWarehouseWarnController extends BaseController
{
    /**
     * @Author:      lyt
     * @DateTime:    2016-06-27 17:22:47
     * @Description: 获取用户的配置的 库存预警信息
     */
    public function lists(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $page_size=Input::get("page_size",10);
            $page=Input::get("page",1);
            $car_retrieval_code=Input::get("car_retrieval_code");
            $type=Input::get("type");
            $search_retrieval_code=Input::get("retrieval_code");
            $status=Input::get("status");            

            $car_str=Input::get("car_str");
            if($type||$search_retrieval_code||$car_str){
                // $non_ids=SupplyItemNonStandard::retrievalCode($sc_user->shop_id,$car_retrieval_code,$type,$search_retrieval_code,$car_str);//获得非标的耗材id范围
                $si_ids=SupplyItem::retrievalCode($car_retrieval_code,$type,$search_retrieval_code,$car_str);//获得标准的耗材id范围
            }


            $si_sql="select si.id,si.type from supply_item si where si.`status`=1";
            if(isset($si_ids)){
                $si_ids[]=0;
                $si_sql.=" and si.id in (".implode(",",$si_ids).")";
            }
            // $sins_sql="select sins.id,1 type from supply_item_non_standard sins where sins.`status`=1";
            // if(isset($non_ids)){
            //     $non_ids[]=0;
            //     $sins_sql.=" and sins.id in (".implode(",",$non_ids).")";
            // }
            $temp_sql="FROM  `maintenance_warehouse_warn` mww JOIN (
                ".$si_sql."
                ) temp on temp.id=mww.item_id where mww.shop_id=".$sc_user->shop_id;
            if($status!=""){
                $temp_sql.=" and mww.status=".$status;
            }
            $sql="select count(1) count ".$temp_sql;
            $sql2="select mww.*  ".$temp_sql;      

            $count=DB::select($sql);
            $sql2= $sql2." limit ?,? ";
            $s_item=DB::select($sql2,array($page_size*($page-1),$page_size));
            foreach ($s_item as $_mis) {
                // if($_mis->item_type==0){//说明是标准耗材
                    $si_info=SupplyItem::getCacheItemInfo($_mis->item_id);
                    $_mis->code=$si_info->code;
                    $_mis->old_code=$si_info->old_code;
                    $_mis->brand_name=$si_info->brand_name;
                    $_mis->series_name=$si_info->series_name;
                    $_mis->model=$si_info->model;
                    $_mis->specification=$si_info->specification;
                    $_mis->scf_id=$si_info->st_classifi_id;
                    $_mis->scf_name=$si_info->scf_name;
                    $_mis->unit=$si_info->unit;
                    $_mis->item_name=$si_info->name;
                // }else{
                //     $sins_info=SupplyItemNonStandard::getCacheItemInfo($_mis->item_id);
                //     $_mis->code=$sins_info->code;
                //     $_mis->old_code=$sins_info->old_code;
                //     $_mis->brand_name=$sins_info->brand_name;
                //     $_mis->series_name=$sins_info->series_name;
                //     $_mis->model=$sins_info->model;
                //     $_mis->specification=$sins_info->specification;
                //     $_mis->scf_id=$sins_info->st_classifi_id;
                //     $_mis->scf_name=$sins_info->scf_name;
                //     $_mis->unit=$sins_info->unit;
                // }
                $part_classification = Report::get_part_classification($_mis->scf_id);
                $_mis->scf_one_id=$part_classification['level_1_id'];
                $_mis->scf_one_name=$part_classification['level_1_name'];
                $_mis->scf_two_id=$part_classification['level_2_id'];
                $_mis->scf_two_name=$part_classification['level_2_name'];
                $_mis->scf_thr_id=$part_classification['level_3_id'];
                $_mis->scf_thr_name=$part_classification['level_3_name'];
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>$count[0]->count,"item_info"=>$s_item));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "getSupply", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }




    /**
     * @Author:      lyt
     * @DateTime:    2016-06-28 11:23:22
     * @Description: 用于检索耗材用户查询耗材的能否监控
     */
    public function getSupply(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }

            $car_retrieval_code=Input::get("car_retrieval_code");
            $type=Input::get("type");
            $search_retrieval_code=Input::get("retrieval_code");
            // if($type||$search_retrieval_code){
            //     $non_ids=SupplyItemNonStandard::retrievalCode($sc_user->shop_id,$car_retrieval_code,$type,$search_retrieval_code);//获得非标的耗材id范围
            //     $si_ids=SupplyItem::retrievalCode($car_retrieval_code,$type,$search_retrieval_code);//获得标准的耗材id范围
            // }
            $car_str=Input::get("car_str");
            if($type||$search_retrieval_code||$car_str){
                // $non_ids=SupplyItemNonStandard::retrievalCode($sc_user->shop_id,$car_retrieval_code,$type,$search_retrieval_code,$car_str);//获得非标的耗材id范围
                $si_ids=SupplyItem::retrievalCode($car_retrieval_code,$type,$search_retrieval_code,$car_str);//获得标准的耗材id范围
            }

            $mww=MaintenanceWarehouseWarn::where("shop_id",$sc_user->shop_id)->get();
            $sino_ids_no=array();
            $si_ids_no=array();
            // foreach ($mww as $_mww) {
            //     if($_mww->item_type==0){
            //         $si_ids_no[]=$_mww->item_id;
            //     }else{
            //         $sino_ids_no[]=$_mww->item_id;
            //     }
            // }

            $si_sql="select si.id,si.type,if(isnull(mw.number),0,mw.number) as number from supply_item si
                LEFT JOIN  maintenance_warehouse  mw on mw.item_id=si.id and mw.shop_id=?
                where si.`status`=1";
            if(isset($si_ids)){
                $temp_ids=array_diff($si_ids, $si_ids_no);
                $temp_ids[]=0;
                $si_sql.=" and si.id in (".implode(",",$temp_ids).")";
            }else if(!empty($si_ids_no)){
                $si_sql.=" and si.id not in (".implode(",",$si_ids_no).")";
            }
            // $sins_sql="select sins.id,1 type,mw.number from supply_item_non_standard sins 
            //     JOIN (select * from maintenance_warehouse where item_type=1 and shop_id=?) mw on mw.item_id=sins.id
            //     where sins.`status`=1 and sins.use_status = 1";
            // if(isset($non_ids)){
            //     $temp_ids=array_diff($non_ids, $sino_ids_no);
            //     $temp_ids[]=0;
            //     $sins_sql.=" and sins.id in (".implode(",",$temp_ids).")";
            // }else if(!empty($sino_ids_no)){
            //     $sins_sql.=" and sins.id not in (".implode(",",$sino_ids_no).")";
            // }

            $sql="select * from (
                ".$si_sql."
                ) temp limit 100";
                Log::info($sql);
            $s_item=DB::select($sql,array($sc_user->shop_id));
            foreach ($s_item as $_mis) {
                // if($_mis->type==0){//说明是标准耗材
                    $si_info=SupplyItem::getCacheItemInfo($_mis->id);
                    $_mis->code=$si_info->code;
                    $_mis->old_code=$si_info->old_code;
                    $_mis->brand_name=$si_info->brand_name;
                    $_mis->series_name=$si_info->series_name;
                    $_mis->model=$si_info->model;
                    $_mis->specification=$si_info->specification;
                    $_mis->scf_id=$si_info->st_classifi_id;
                    $_mis->scf_name=$si_info->scf_name;
                    $_mis->unit=$si_info->unit;
                    $_mis->item_name=$si_info->name;
                // }else{
                //     $sins_info=SupplyItemNonStandard::getCacheItemInfo($_mis->id);
                //     $_mis->code=$sins_info->code;
                //     $_mis->old_code=$sins_info->old_code;
                //     $_mis->brand_name=$sins_info->brand_name;
                //     $_mis->series_name=$sins_info->series_name;
                //     $_mis->model=$sins_info->model;
                //     $_mis->specification=$sins_info->specification;
                //     $_mis->scf_id=$sins_info->st_classifi_id;
                //     $_mis->scf_name=$sins_info->scf_name;
                //     $_mis->unit=$sins_info->unit;
                // }
                $part_classification = Report::get_part_classification($_mis->scf_id);
                $_mis->scf_one_id=$part_classification['level_1_id'];
                $_mis->scf_one_name=$part_classification['level_1_name'];
                $_mis->scf_two_id=$part_classification['level_2_id'];
                $_mis->scf_two_name=$part_classification['level_2_name'];
                $_mis->scf_thr_id=$part_classification['level_3_id'];
                $_mis->scf_thr_name=$part_classification['level_3_name'];
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>sizeof($s_item),"item_info"=>$s_item));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "getSupply", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-06-28 13:08:27
     * @Description: 添加一个耗材作为监控
     */
    public function addSupply(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                // 'type' => 'required',
                'id' => 'required',
                'max_number' => 'required',
                'mim_number' => 'required',
                'status' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            // if(Input::get("type")==0){
                $supply=SupplyItem::find(Input::get("id"));
            // }else{
            //     $supply=SupplyItemNonStandard::find(Input::get("id"));
            //     if($supply->use_status==0){
            //         $response = Lang::get("scs_error.supply_item_not_exit_41007");
            //         break;
            //     }
            // }
            if(!$supply){
                $response = Lang::get("scs_error.supply_item_not_exit_41000");
                break;
            }
            if(Input::get("mim_number")<=0){
                $response = Lang::get("scs_error.warn_number_small_error_10007");
                break;
            }
            if(Input::get("max_number")<Input::get("mim_number")){
                $response = Lang::get("scs_error.warn_number_error_10006");
                break;
            }
            $mww=MaintenanceWarehouseWarn::where("shop_id",$sc_user->shop_id)->where("item_id",$supply->id)->first();
            if($mww){
                $response = Lang::get("scs_error.warn_supply_error_10008");
                break;  
            }
            $wmm=new MaintenanceWarehouseWarn;
            $wmm->item_type=$supply->type;
            $wmm->item_id=$supply->id;
            $wmm->shop_id=$sc_user->shop_id;
            $wmm->max_number=Input::get("max_number");
            $wmm->mim_number=Input::get("mim_number");
            $wmm->status=Input::get("status");
            $wmm->save();            
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("id"=>$wmm->id));            
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "addSupply", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      lyt
     * @DateTime:    2016-06-28 13:50:14
     * @Description: 用于修改 耗材的预警配置的信息
     */
    public function editSupplyWarn(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required',
                'max_number' => 'required',
                'mim_number' => 'required',
                'status' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $mww=MaintenanceWarehouseWarn::find(Input::get("id"));
            if($mww->item_type==1){
                $sins=SupplyItem::find($mww->item_id);
                if($sins->status==0 && Input::get('status')==1){
                    $response = Lang::get("scs_error.warn_supply_error_10010");
                    break;
                }
            }
            if(!$mww||$mww->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.supply_warn_not_exit_10009");
                break;
            }
            if(Input::get("mim_number")<=0){
                $response = Lang::get("scs_error.warn_number_small_error_10007");
                break;
            }
            if(Input::get("max_number")<Input::get("mim_number")){
                $response = Lang::get("scs_error.warn_number_error_10006");
                break;
            }
            $mww->max_number=Input::get("max_number");
            $mww->mim_number=Input::get("mim_number");
            $mww->status=Input::get("status");
            $mww->save();            
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("id"=>$mww->id));            
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "editSupplyWarn", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-06-28 14:02:50
     * @Description: 获取达到预警值的 耗材信息
     */
    public function warnSupplyLists(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $page_size=Input::get("page_size",10);
            $page=Input::get("page",1);
            $car_retrieval_code=Input::get("car_retrieval_code");
            $type=Input::get("type");
            $search_retrieval_code=Input::get("retrieval_code");
            $status=Input::get("status");
            // if($type||$search_retrieval_code){
            //     $non_ids=SupplyItemNonStandard::retrievalCode($sc_user->shop_id,$car_retrieval_code,$type,$search_retrieval_code);//获得非标的耗材id范围
            //     $si_ids=SupplyItem::retrievalCode($car_retrieval_code,$type,$search_retrieval_code);//获得标准的耗材id范围
            // }

            $car_str=Input::get("car_str");
            if($type||$search_retrieval_code||$car_str){
                // $non_ids=SupplyItemNonStandard::retrievalCode($sc_user->shop_id,$car_retrieval_code,$type,$search_retrieval_code,$car_str);//获得非标的耗材id范围
                $si_ids=SupplyItem::retrievalCode($car_retrieval_code,$type,$search_retrieval_code,$car_str);//获得标准的耗材id范围
            }

            $si_sql="select si.id,si.type,if(isnull(mw.number),0,mw.number) as number from supply_item si
                LEFT JOIN  maintenance_warehouse  mw on mw.item_id=si.id and mw.shop_id=?
                where si.`status`=1";
            if(isset($si_ids)){
                $si_ids[]=0;
                $si_sql.=" and si.id in (".implode(",",$si_ids).")";
            }
            // $sins_sql="select sins.id,1 type,mw.number from supply_item_non_standard sins 
            //     LEFT JOIN (select * from maintenance_warehouse where item_type=1 and shop_id=?) mw on mw.item_id=sins.id
            //     where sins.`status`=1 and sins.use_status = 1";
            // if(isset($non_ids)){
            //     $non_ids[]=0;
            //     $sins_sql.=" and sins.id in (".implode(",",$non_ids).")";
            // }
            $temp_sql="FROM  `maintenance_warehouse_warn` mww JOIN (
                ".$si_sql."
                ) temp on temp.id=mww.item_id where  mww.status=1 and (mww.mim_number>temp.number or mww.max_number<temp.number) and mww.shop_id=".$sc_user->shop_id;
            
            $sql="select count(1) count ".$temp_sql;
            $sql2="select mww.*,temp.number  ".$temp_sql;

            $count=DB::select($sql,array($sc_user->shop_id));
            $sql2= $sql2." limit ?,? ";
            $s_item=DB::select($sql2,array($sc_user->shop_id,$page_size*($page-1),$page_size));
            foreach ($s_item as $_mis) {
                // if($_mis->item_type==0){//说明是标准耗材
                    $si_info=SupplyItem::getCacheItemInfo($_mis->item_id);
                    $_mis->code=$si_info->code;
                    $_mis->old_code=$si_info->old_code;
                    $_mis->brand_name=$si_info->brand_name;
                    $_mis->series_name=$si_info->series_name;
                    $_mis->model=$si_info->model;
                    $_mis->specification=$si_info->specification;
                    $_mis->scf_id=$si_info->st_classifi_id;
                    $_mis->scf_name=$si_info->scf_name;
                    $_mis->unit=$si_info->unit;
                    $_mis->car_model='';
                    $_mis->car_model_comments=$si_info->car_model_comments;
                    $_mis->item_name=$si_info->name;
                // }else{
                //     $sins_info=SupplyItemNonStandard::getCacheItemInfo($_mis->item_id);
                //     $_mis->code=$sins_info->code;
                //     $_mis->old_code=$sins_info->old_code;
                //     $_mis->brand_name=$sins_info->brand_name;
                //     $_mis->series_name=$sins_info->series_name;
                //     $_mis->model=$sins_info->model;
                //     $_mis->specification=$sins_info->specification;
                //     $_mis->scf_id=$sins_info->st_classifi_id;
                //     $_mis->scf_name=$sins_info->scf_name;
                //     $_mis->unit=$sins_info->unit;
                //     $_mis->car_model=$sins_info->car_model;
                //     $_mis->car_model_comments=$sins_info->car_model;
                // }
                $part_classification = Report::get_part_classification($_mis->scf_id);
                $_mis->scf_one_id=$part_classification['level_1_id'];
                $_mis->scf_one_name=$part_classification['level_1_name'];
                $_mis->scf_two_id=$part_classification['level_2_id'];
                $_mis->scf_two_name=$part_classification['level_2_name'];
                $_mis->scf_thr_id=$part_classification['level_3_id'];
                $_mis->scf_thr_name=$part_classification['level_3_name'];
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>$count[0]->count,"item_info"=>$s_item));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "getSupply", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author   lyt                      <ahlyt@anhuibj.com>
     * @DateTime 2018-01-31T10:02:37+0800
     * @return   [type]                   [description]
     * 低于底限的 库存预警信息
     */
    public function minWarnSupplyLists(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
           
           
            $temp_sql="select mww.id,mww.item_type,mww.item_id,mww.max_number,mww.mim_number,temp.number  FROM  `maintenance_warehouse_warn` mww JOIN (
                select si.id,si.type,if(isnull(mw.number),0,mw.number) as number from supply_item si
                LEFT JOIN  maintenance_warehouse  mw on mw.item_id=si.id and mw.shop_id=?
                where si.`status`=1";
            if(Input::get("level_3_id")){
                $temp_sql.=(" and si.supply_classification_id=".Input::get("level_3_id"));
            }
            $dis=") temp on temp.id=mww.item_id where  mww.status=1 and mww.mim_number>temp.number and mww.shop_id=?";            
            $temp_sql.=$dis;
            $s_item=DB::select($temp_sql,array($sc_user->shop_id,$sc_user->shop_id));
            foreach ($s_item as $_mis) {
                // if($_mis->item_type==0){//说明是标准耗材
                    $si_info=SupplyItem::getCacheItemInfo($_mis->item_id);
                    $_mis->demand_quantity=round($_mis->mim_number-$_mis->number,2); //上限 减去库存等于 需求量
                    $_mis->code=$si_info->code;
                    $_mis->old_code=$si_info->old_code;
                    $_mis->brand_name=$si_info->brand_name;
                    $_mis->series_name=$si_info->series_name;
                    $_mis->model=$si_info->model;
                    $_mis->specification=$si_info->specification;
                    $_mis->scf_id=$si_info->st_classifi_id;
                    $_mis->scf_name=$si_info->scf_name;
                    $_mis->unit=$si_info->unit;
                    $_mis->car_model='';
                    $_mis->car_model_comments=$si_info->car_model_comments;
                    $_mis->item_name=$si_info->name;

                    $ss=SupplyStorage::where("origin_id",$si_info->id)->where("status",1)->orderBy("id","desc")->first();//暂时直接获取指定 耗材的最新转换关系 数据量小建议 数据量大 建议该表cache id 对应耗材关系
                    if(isset($ss->data_id)){//如果存在转换关系  
                        $storage_si_info=SupplyItem::getCacheItemInfo($ss->data_id);
                        $_mis->storage_item_id=$storage_si_info->id;
                        $_mis->storage_item_type=$storage_si_info->type;
                        $_mis->storage_demand_quantity=floor(($_mis->mim_number-$_mis->number)*$ss->num); //需要采购数量
                        $_mis->storage_code=$storage_si_info->code;
                        $_mis->storage_old_code=$storage_si_info->old_code;
                        $_mis->storage_brand_name=$storage_si_info->brand_name;
                        $_mis->storage_series_name=$storage_si_info->series_name;
                        $_mis->storage_model=$storage_si_info->model;
                        $_mis->storage_specification=$storage_si_info->specification;
                        $_mis->storage_scf_id=$storage_si_info->st_classifi_id;
                        $_mis->storage_scf_name=$storage_si_info->scf_name;
                        $_mis->storage_unit=$storage_si_info->unit;
                        $_mis->storage_car_model='';
                        $_mis->storage_car_model_comments=$storage_si_info->car_model_comments;
                        $_mis->storage_item_name=$storage_si_info->name;
                    }else{
                        $_mis->storage_item_id=$si_info->id;
                        $_mis->storage_item_type=$si_info->type;
                        $_mis->storage_demand_quantity=floor($_mis->mim_number-$_mis->number); //需要采购数量
                        $_mis->storage_code=$si_info->code;
                        $_mis->storage_old_code=$si_info->old_code;
                        $_mis->storage_brand_name=$si_info->brand_name;
                        $_mis->storage_series_name=$si_info->series_name;
                        $_mis->storage_model=$si_info->model;
                        $_mis->storage_specification=$si_info->specification;
                        $_mis->storage_scf_id=$si_info->st_classifi_id;
                        $_mis->storage_scf_name=$si_info->scf_name;
                        $_mis->storage_unit=$si_info->unit;
                        $_mis->storage_car_model='';
                        $_mis->storage_car_model_comments=$si_info->car_model_comments;
                        $_mis->storage_item_name=$si_info->name;
                    }
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>sizeof($s_item),"item_info"=>$s_item));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "minWarnSupplyLists", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author   lyt                      <ahlyt@anhuibj.com>
     * @DateTime 2018-01-31T13:38:38+0800
     * @return   [type]                   [description]
     * 超库存的上限耗材数量
     */
    public function maxWarnSupplyLists(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $temp_sql="select mww.id,mww.item_type,mww.item_id,mww.max_number,mww.mim_number,temp.number  FROM  `maintenance_warehouse_warn` mww JOIN (
                select si.id,si.type,if(isnull(mw.number),0,mw.number) as number from supply_item si
                LEFT JOIN  maintenance_warehouse  mw on mw.item_id=si.id and mw.shop_id=?
                where si.`status`=1";
            if(Input::get("level_3_id")){
                $temp_sql.=(" and si.supply_classification_id=".Input::get("level_3_id"));
            }
            $dis=") temp on temp.id=mww.item_id where  mww.status=1 and mww.max_number<temp.number and mww.shop_id=?";            
            $temp_sql.=$dis;
            $s_item=DB::select($temp_sql,array($sc_user->shop_id,$sc_user->shop_id));
            foreach ($s_item as $_mis) {
                $si_info=SupplyItem::getCacheItemInfo($_mis->item_id);
                $_mis->demand_quantity=round($_mis->number-$_mis->max_number,2); //超限数量
                $_mis->code=$si_info->code;
                $_mis->old_code=$si_info->old_code;
                $_mis->brand_name=$si_info->brand_name;
                $_mis->series_name=$si_info->series_name;
                $_mis->model=$si_info->model;
                $_mis->specification=$si_info->specification;
                $_mis->scf_id=$si_info->st_classifi_id;
                $_mis->scf_name=$si_info->scf_name;
                $_mis->unit=$si_info->unit;
                $_mis->car_model='';
                $_mis->car_model_comments=$si_info->car_model_comments;
                $_mis->item_name=$si_info->name;              
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>sizeof($s_item),"item_info"=>$s_item));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "maxWarnSupplyLists", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

}